package com.feri.fc.listener;

import com.feri.fc.config.RocketMQConfig;
import com.feri.fc.dao.OrderDao;
import com.feri.fc.dao.OrderLogDao;
import com.feri.fc.entity.Order;
import com.feri.fc.entity.OrderLog;
import com.feri.fc.type.OrderStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 指尖造梦，精益求精！
 *
 * @Author：Feri
 * @Date：2024/12/2 10:15
 * @Modified：
 */
@Component
@Slf4j
@RequiredArgsConstructor
@RocketMQMessageListener(topic = RocketMQConfig.TOPIC_ORDERTO,consumerGroup = "ordertimeout")
public class OrderTimeOutListener implements RocketMQListener<Integer> {
    private final OrderDao dao;
    private final OrderLogDao logDao;

    @Override
    public void onMessage(Integer id) {
        log.info("开始处理，预约陪诊订单超时信息");
        //查询 订单的状态，如果是未付款，自动超时
        Order order=dao.selectById(id);
        if(order.getStatus().equals(OrderStatus.NOPAY.getValue())){
            //超时
            order.setStatus(OrderStatus.TIMEOUT.getValue());
            order.setUpdateTime(new Date());
            dao.updateById(order);
            logDao.insert(new OrderLog(order.getId(),order.getStatus(),"陪诊服务预约订单超时未支付，自动取消订单！"));
        }
        log.info("结束处理，预约陪诊订单超时信息");
    }
}
